
//文件选择
var inputBox = document.getElementById("inputBox")

//图片base64输入框
var imgBase64Text = document.getElementById("imgBase64Text")

//转图片按钮
var toImgBtn = document.getElementById("toImg")

//图片标签id
var imgid = document.getElementById("imgid")

//复制base64 按钮
var coypBtn = document.getElementById("copyBtn")

//清除按钮
var clearBtn = document.getElementById("clearbtn")

//图片转成base64标签
var base64L = document.getElementById("ptest")

/**
 选择文件
 */
inputBox.onchange = function(){
    var reader = new FileReader()
    reader.readAsDataURL(this.files[0])
    reader.onload = function(){
        console.log(reader.result)
        document.getElementById("ptest").innerHTML = reader.result
    }
}

/**
 清除base64输入内容
 */
clearBtn.onclick = function(){
    document.getElementById("ptest").innerHTML = ""
}

/*
 图片base64转图片
 */
toImgBtn.onclick = function(){
    let imgBase64 = document.getElementById("imgBase64Text").value
    imgid.src = imgBase64
}

/*
 复制base64内容
 */
coypBtn.onclick = function(){
    console.log("进来了copybtn")
    let base64 =  document.getElementById("ptest").innerHTML
    console.log("base64",base64)

    if (base64 == null || base64 == "") {
        alert("不能为空")
    }
    //复制内容
    copyText(base64)
}

/*
 复制
 */
function copyText(text) {
  if (!navigator.clipboard) {
    console.log('该浏览器不支持Clipboard API')
    return
  }
  navigator.clipboard.writeText(text).then(function() {
    console.log('复制成功')
  }, function() {
    console.log('复制失败')
  })
}
